﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Entities;
using DAL;
using Model.Account;
using System.Net;
using Syncar.Web.FrontEnd.Models;
using Model.Helpers;
using System.Transactions;

namespace Syncar.Web.FrontEnd.Controllers
{ 
    [Authorize]
    public class UserVehicleController : ImageController
    {
        private SyncarContext db = new SyncarContext();

        //
        // GET: /UserVehicle/

        private User GetLoggedUser()
        {
            //Load the user with his vehicles
            return db.FindUserByUserName(User.Identity.Name).Include(x => x.UserVehicles).FirstOrDefault();
        }

        public ViewResult Index()
        {
            var user = GetLoggedUser();
            return View(user.UserVehicles.ToList());
        }

        //
        // GET: /UserVehicle/Details/5

        public ViewResult Details(int id)
        {
            UserVehicle uservehicle = db.UserVehicles.Find(id);
            return View(uservehicle);
        }

        //
        // GET: /UserVehicle/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // GET: /UserVehicle/Cancel

        [HttpGet, ActionName("Cancel")]
        public ActionResult CancelEdition()
        {
            return RedirectToAction("Index");
        } 

        //
        // POST: /UserVehicle/Create

        [HttpPost]
        public ActionResult Create(UserVehicle uservehicle)
        {
            var user = GetLoggedUser();
            uservehicle.User = user;

            if (ModelState.IsValid)
            {                
                db.UserVehicles.Add(uservehicle);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(uservehicle);
        }
        
        //
        // GET: /UserVehicle/Edit/5
 
        public ActionResult Edit(int id)
        {
            var user = GetLoggedUser();
            if(!user.HasVehicle(id))
                throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");
            UserVehicle uservehicle = db.UserVehicles.Find(id);
            db.Entry(uservehicle).Collection(u => u.Images).Load();
            return View(uservehicle);
        }

        //
        // POST: /UserVehicle/Edit/5

        [HttpPost]
        public ActionResult Edit(UserVehicle uservehicle)
        {
            if (ModelState.IsValid)
            {
                var user = GetLoggedUser();
                if (!user.HasVehicle(uservehicle.UserVehicleID))
                    throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");
            
                db.Entry(uservehicle).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(uservehicle);
        }

        //
        // GET: /UserVehicle/Delete/5
 
        public ActionResult Delete(int id)
        {
            var user = GetLoggedUser();
            if (!user.HasVehicle(id))
                throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");
            
            UserVehicle uservehicle = db.UserVehicles.Find(id);
            return View(uservehicle);
        }

        //
        // POST: /UserVehicle/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var user = GetLoggedUser();
            if (!user.HasVehicle(id))
                throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");
            
            UserVehicle uservehicle = db.UserVehicles.Find(id);
            db.UserVehicles.Remove(uservehicle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public override ActionResult UploadImage(UploadImageModel imageModel)
        {
            if (ModelState.IsValid)
            {
                var user = GetLoggedUser();
                if (!user.HasVehicle(imageModel.EntityID))
                    throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");
                
                UserVehicle uservehicle = db.UserVehicles.Find(imageModel.EntityID);
                db.Entry(uservehicle).Collection(u => u.Images).Load();
                var image = db.Images.Create();
                image.SetRelativePath(user.Name);
                imageModel.Path = image.Path;

                try
                {
                    if (this.SaveToFilteSystem(imageModel))
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            db.Images.Add(image);
                            db.SaveChanges();

                            uservehicle.Images.Add(image);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex) { }
                return RedirectToAction("Edit", new { id = imageModel.EntityID });
            }
            return View("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}